/**
 * @Created by zhuly8 zhuly8@yusys.com.cn on 2019-8-5 09:59:16.
 * @updated by
 * @description 应用系统管理
 */
define(
	function (require, exports) {
		/**
		 * 页面加载完成时触发
		 * @param hashCode 路由ID
		 * @param data 传递数据对象
		 * @param cite 页面站点信息
		 */
		exports.ready = function (hashCode, data, cite) {
			yufp.lookup.reg('USE_ALERTM_MODE');
			yufp.custom.vue({
				el: cite.el,
				data: function () {
					/*定义校验规则，只能填字母数字 下划线*/
					var valiCodes = function (rule, value, callback) {
						var reg = /^[0-9a-zA-Z_]{1,}$/;
						if (value && reg.test(value)) {
							if (value.length > 64) {
								callback(new Error('长度不能大于64位'));
							} else {
								callback();
							}
						} else if (value && !reg.test(value)) {
							callback(new Error('能填字母、数字和下划线'));
						} else {
							callback();
						}
					};
					return {
						tbHeight:'0',
						rulesone: [
							{ validator: valiCodes, trigger: 'blur' }
						],
						searchFormdata: {},
						addFnButton: !yufp.session.checkCtrl('addFn'),
						updateFnButton: !yufp.session.checkCtrl('updateFn'),
						deleteFnButton: !yufp.session.checkCtrl('deleteFn'),
						dataUrl: 'api/use/warnings/users/queryList',
						saveBtnShow: true,
						cancelBtnShow: true,
						formdata: {},
						isReadOnly: false,
						saveDisabled: false,
						async: false,
						height: yufp.frame.size().height,
						dialogVisible: false,
						viewTitleArrry: ['预警人员新增', '预警人员修改', '预警人员查看'],
						viewTitle: '',
						addFlag: true
					};
				},
				methods: {

					checkEmail: function (email) {
						var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
						if (reg.test(email)) {
							return true
						} else {
							return false;
						}

					},
					checkPhone: function (phone) {
						var reg = /^1[3456789]\d{9}$/;
						if (reg.test(phone)) {
							return true
						} else {
							return false;
						}

					},

					/**
					 * 取消
					 */
					cancelFn: function () {
						var _this = this;
						_this.clearAllFn();
						_this.dialogVisible = false;
					},
					/**
					 * 保存
					 */
					saveFn: function () {
						var _this = this;
						var validate = false;
						_this.$refs.refForm.validate(function (valid) {
							validate = valid;
						});
						if (!validate) {
							return;
						}

						var paramObj = _this.$refs.refForm.formdata;
						if (paramObj.alarmMode === 'M') {
							if (!_this.checkEmail(paramObj.targetData)) {
								_this.$message({ message: "通知地址邮箱不合法，请检查", type: "warning" });
								return;
							}
						}
						// if (paramObj.alarmMode === 'S') {
						// 	if (!_this.checkPhone(paramObj.targetData)) {
						// 		_this.$message({ message: "通知地址手机号不合法，请检查", type: "warning" });
						// 		return;
						// 	}
						// }//手机号不再校验
						let url = "";
						if (_this.addFlag) {
							url = 'api/use/warnings/users/addWarningUser';
						} else {
							url = 'api/use/warnings/users/updateWarningUser';
						}

						yufp.service.request({
							method: 'POST',
							url: url,
							data: paramObj,
							callback: function (code, message, response) {
								if (response.code != 0) {
									_this.$message(response.message);
								} else {
									_this.$refs.refTable.remoteData();
									_this.$message('操作成功');
									_this.dialogVisible = false;
								}

							}
						});

					},
					/**
					 * 控制保存按钮、xdialog、表单的状态
					 * @param viewType 表单类型
					 * @param editable 可编辑,默认false
					 */
					switchStatus: function (viewType, editable) {
						var _this = this;
						_this.viewType = viewType;
						_this.dialogVisible = true;
						_this.saveBtnShow = editable;
						_this.cancelBtnShow = editable;
					},
					/**
					 * 新增
					 */
					addFn: function () {
						var _this = this;
						_this.dialogVisible = true;
						_this.viewTitle = _this.viewTitleArrry[0];
						_this.addFlag = true;
						_this.saveDisabled = false;
						_this.$nextTick(function () {
							_this.viewTitle = _this.viewTitleArrry[0];
							_this.clearAllFn();
						});
					},

					clearAllFn: function () {
						var _this = this;
						_this.$refs.refForm.resetFields();
						_this.isReadOnly = false;
					},

					/**
					 * 修改
					 */
					modifyFn: function () {
						var _this = this;
						if (_this.$refs.refTable.selections.length != 1) {
							_this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
							return;
						}
						_this.viewTitle = _this.viewTitleArrry[1];
						_this.dialogVisible = true;
						_this.addFlag = false;
						_this.saveDisabled = false;
						_this.isReadOnly = true;
						_this.$nextTick(function () {
							_this.clearAllFn();

							_this.formdata.targetCode = _this.$refs.refTable.selections[0].targetCode;
							_this.formdata.targetName = _this.$refs.refTable.selections[0].targetName;
							_this.formdata.alarmMode = _this.$refs.refTable.selections[0].alarmMode;
							_this.formdata.remark = _this.$refs.refTable.selections[0].remark;
							_this.formdata.targetData = _this.$refs.refTable.selections[0].targetData;

						})
					},

					viewFn: function (obj) {

						var _this = this;

						_this.viewTitle = _this.viewTitleArrry[2];
						_this.dialogVisible = true;
						_this.addFlag = false;
						_this.saveDisabled = true;
						_this.$nextTick(function () {
							_this.clearAllFn();


							_this.formdata.targetCode = obj.targetCode;
							_this.formdata.targetName = obj.targetName;
							_this.formdata.alarmMode = obj.alarmMode;
							_this.formdata.remark = obj.remark;
							_this.formdata.targetData = obj.targetData;
							// _this.isReadOnly =true;

						})

					},



					/**
					 * 删除
					 */
					deleteFN: function () {
						var _this = this;
						var selections = _this.$refs.refTable.selections;
						if (selections.length != 1) {
							_this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
							return;
						}
						_this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning', duration: 6000,
							center: true,
							callback: function (action) {
								if (action === 'confirm') {
									yufp.service.request({
										method: 'DELETE',
										url: 'api/use/warnings/users/deleteWarningUser',
										data: _this.$refs.refTable.selections[0],
										callback: function (code, message, response) {
											if (response.code == 0) {
												_this.$refs.refTable.remoteData();
												_this.$message('操作成功');
											} else {
												_this.$message(response.message);
											}
										}
									});
								}
							}
						});
					},


				},
				mounted: function () {
					var _this = this;
					//测试
					_this.$nextTick(function(){
							var tableBox = _this.$refs.tableBox;
							let a = tableBox.getBoundingClientRect().top + 28;
							var theight = `calc(100vh - ${a}px - 64px)`;
							_this.tbHeight = theight;		
					});
				}
			});
		};
	});